"""
和文件相关的类定义
"""
from tensorflow.python.saved_model.main_op_impl import main_op
import  json
from data_define import Record


#定义一个抽象类，用于做顶层设计，确定有哪些功能需要实现
class FileReader:
    def read_data(self)->list[Record]:
        """"将读到的每一条数据转换为Record类对象，将它们都封装到list内返回即可"""
        pass
class TextFileReader(FileReader):
    def __init__(self,path):
        self.path=path#定义成员变量记录文件路径
    #复写(实现抽象方法)父类方法
    def read_data(self)->list[Record]:
        f=open(self.path,"r",encoding="UTF-8")
        record_list:list[Record]=[]
        for line in f.readlines():
            line=line.strip()#消除每一行读取到的\n
            data_list=line.split("、")
            print(line)
            record=Record(data_list[0])
            record_list.append(record)
        f.close()
        return record_list

class JsonFileReader(FileReader):
    def __init__(self,path):
        self.path=path
    def read_data(self) ->list[Record]:
        f=open(self.path,"r",encoding="UTF-8")
        record_list:list[Record]=[]
        for line in f.readlines():
            data_dict=json.loads(line)
            record=Record(data_dict["data"],data_dict["order_id"],int(data_dict["money"]),data_dict["province"])
            record_list.append(record)


if __name__ == '__main__':
    #文本文件读取
    text_file_reader=TextFileReader("C:/test.txt/魔女.txt")
    text_file_reader.read_data()
    #json文件读取
    # JsonFileReader